package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.goods

import android.content.Context
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.alibaba.fastjson.JSON
import com.example.library_common.db.ShopCarBean
import com.example.library_common.db.UserDatabase
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.GoodsDetailBean
import com.example.wallet.global.AppGlobals
import com.example.wallet.utils.selectorIcon

class GoodsNewDetailViewModel : ViewModel() {

    val icon = AppGlobals.getApplication().getString(R.string.wallet_sst)

    val cml = selectorIcon()


    val infoDaoLiveData = MutableLiveData<GoodsDetailBean>()

    val mShopCarLiveData = MutableLiveData<ShopCarBean>()

    val collectionLiveData = MutableLiveData<Boolean>()

    var mCategoryId: String = ""

    val mRepository = GoodsNewDetailRepository()

    fun getGoodsDetailInfo() =
        mRepository.getGoodsDetailInfo(this)


    fun getLocalShopCarBean() {
        mShopCarLiveData.postValue(mRepository.getLocalShopCarBean())
    }

    fun saveGoods(context: Context) {
        val mShopCar = mShopCarLiveData.value

        val shopCarList = mShopCar?.shopCarList22

        val infoDao = infoDaoLiveData.value

        var flag = false

        var goods = infoDao?.goods

        if (shopCarList != null) {
            for (e in shopCarList) {

                if (e == goods) {

                    flag = true

                    goods = e
                    break
                }
            }
        }
//
        if (!flag) {
            shopCarList?.add(goods)
        } else {
            val selectedCount = goods?.selectedCount ?: 0
            goods?.selectedCount = selectedCount.plus(1)
        }
        mShopCar?.shopCarList = JSON.toJSONString(shopCarList)

        UserDatabase.getInstance()
            .shopCarBean()
            .save(mShopCar)

        Toast.makeText(context, "加入购物车成功", Toast.LENGTH_SHORT).show()
    }

//    fun moneyTvText() =
//        "${goodsPriceText()} 赠送 ${infoDaoLiveData.value?.goods?.givenCml} $cml"

    fun moneyTvText():String{
        val context = AppGlobals.getApplication()
        val givenCml="${infoDaoLiveData.value?.goods?.givenCml}"
        return "${goodsPriceText()} ${context.getString(R.string.wallet_fu_li_scored)}"
//        return "${goodsPriceText()} ${context.getString(R.string.wallet_fu_li_scored,givenCml)}"
//        val suffix=AppGlobals.getApplication().getString(R.string.wallet_fu_li_scored)
//        return "${goodsPriceText()} 赠送 ${infoDaoLiveData.value?.goods?.givenCml} $suffix"
    }

    fun goodsPriceText() = "${infoDaoLiveData.value?.goods?.goodsPrice} $icon"

    fun goodsOldPriceText() = "原价:${infoDaoLiveData.value?.goods?.goodsOldPrice} $icon"

    fun goodsStockText() = "库存：${infoDaoLiveData.value?.goods?.goodsStock} 件"

    fun goodsSalesText() = "销量：${infoDaoLiveData.value?.goods?.goodsSales} 件"

    fun collectionHandle() = mRepository.collectionHandle(this)
}